package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.basic.StorehouseAreaPlatMapper;
import com.canaan.business.domain.basic.StorehouseAreaPlat;
import com.canaan.business.service.basic.IStorehouseAreaPlatService;

/**
 * 库区绘制地图信息Service业务层处理
 *
 * @author 
 * @date 2023-08-04
 */
@Service
public class StorehouseAreaPlatServiceImpl implements IStorehouseAreaPlatService {
    @Resource
    private StorehouseAreaPlatMapper storehouseAreaPlatMapper;

    /**
     * 查询库区绘制地图信息
     *
     * @param id 库区绘制地图信息主键
     * @return 库区绘制地图信息
     */
    @Override
    public StorehouseAreaPlat selectStorehouseAreaPlatById(Long id) {
        return storehouseAreaPlatMapper.selectStorehouseAreaPlatById(id);
    }

    /**
     * 查询库区绘制地图信息列表
     *
     * @param storehouseAreaPlat 库区绘制地图信息
     * @return 库区绘制地图信息
     */
    @Override
    public List<StorehouseAreaPlat> selectStorehouseAreaPlatList(StorehouseAreaPlat storehouseAreaPlat) {
        return storehouseAreaPlatMapper.selectStorehouseAreaPlatList(storehouseAreaPlat);
    }

    /**
     * @Description 根据库区编号获取属性
     * @author canaan
     * @date 2023年08月07日 13:52:52
     * @param storehouseAreaNo
     * @return
     */
    @Override
    public StorehouseAreaPlat getInfoByStorehouseAreaNo(String storehouseAreaNo) {
        StorehouseAreaPlat plat = new StorehouseAreaPlat();
        plat.setStorehouseAreaNo(storehouseAreaNo);
        List<StorehouseAreaPlat> platList = storehouseAreaPlatMapper.selectStorehouseAreaPlatList(plat);
        if (CollectionUtil.isNotEmpty(platList)){
            return platList.get(0);
        }
        return null;
    }

    /**
     * 新增库区绘制地图信息
     *
     * @param storehouseAreaPlat 库区绘制地图信息
     * @return 结果
     */
    @Override
    public int insertStorehouseAreaPlat(StorehouseAreaPlat storehouseAreaPlat) {
        StorehouseAreaPlat plat = this.getInfoByStorehouseAreaNo(storehouseAreaPlat.getStorehouseAreaNo());
        if (plat!=null){
            throw new ServiceException("该库区的属性已存在");
        }
        storehouseAreaPlat.setCreateBy(SecurityUtils.getUsername());
        storehouseAreaPlat.setCreateTime(DateUtils.getNowDate());
        return storehouseAreaPlatMapper.insertStorehouseAreaPlat(storehouseAreaPlat);
    }

    /**
     * 批量新增库区绘制地图信息
     *
     * @param storehouseAreaPlatList 库区绘制地图信息列表
     * @return 结果
     */
    @Override
    public int batchInsertStorehouseAreaPlat(List<StorehouseAreaPlat> storehouseAreaPlatList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseAreaPlat storehouseAreaPlat : storehouseAreaPlatList){
            storehouseAreaPlat.setCreateBy(username);
            storehouseAreaPlat.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<storehouseAreaPlatList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseAreaPlatList.size()) {
                endIndex = storehouseAreaPlatList.size();
            }
            insertNum = insertNum + storehouseAreaPlatMapper.batchInsertStorehouseAreaPlat(storehouseAreaPlatList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库区绘制地图信息
     *
     * @param storehouseAreaPlat 库区绘制地图信息
     * @return 结果
     */
    @Override
    public int updateStorehouseAreaPlat(StorehouseAreaPlat storehouseAreaPlat) {
        storehouseAreaPlat.setUpdateBy(SecurityUtils.getUsername());
        storehouseAreaPlat.setUpdateTime(DateUtils.getNowDate());
        return storehouseAreaPlatMapper.updateStorehouseAreaPlat(storehouseAreaPlat);
    }

    /**
     * 批量修改库区绘制地图信息
     *
     * @param storehouseAreaPlatList 库区绘制地图信息列表
     * @return 结果
     */
    @Override
    public int batchUpdateStorehouseAreaPlat(List<StorehouseAreaPlat> storehouseAreaPlatList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseAreaPlat storehouseAreaPlat : storehouseAreaPlatList){
            storehouseAreaPlat.setUpdateBy(username);
            storehouseAreaPlat.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<storehouseAreaPlatList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseAreaPlatList.size()) {
                endIndex = storehouseAreaPlatList.size();
            }
            updateNum = updateNum + storehouseAreaPlatMapper.batchUpdateStorehouseAreaPlat(storehouseAreaPlatList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库区绘制地图信息
     *
     * @param ids 需要删除的库区绘制地图信息主键集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaPlatByIds(Long[] ids) {
        return storehouseAreaPlatMapper.deleteStorehouseAreaPlatByIds(ids);
    }

    /**
     * 删除库区绘制地图信息信息
     *
     * @param id 库区绘制地图信息主键
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaPlatById(Long id) {
        return storehouseAreaPlatMapper.deleteStorehouseAreaPlatById(id);
    }

    @Override
    public boolean uploadStoreHouseArea(String storeHouseAreaNo, String url) {
        if (StringUtils.isNotEmpty(storeHouseAreaNo)){
            StorehouseAreaPlat plat = this.getInfoByStorehouseAreaNo(storeHouseAreaNo);
            if (plat!=null){
                plat.setPlatImg(url);
                this.storehouseAreaPlatMapper.updateStorehouseAreaPlat(plat);
                return true;
            }
        }
        return false;
    }
}
